package com.shengyin.ec.controller.permission;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.WebUtils;

import com.shengyin.ec.model.entity.Resources;
import com.shengyin.ec.model.entity.Roles;
import com.shengyin.ec.service.permission.ResourcesService;
import com.shengyin.ec.service.permission.RolesService;

@Controller
public class RolesController {
	
	
	@Autowired
	RolesService rolesService;
	
	@Autowired
	ResourcesService resourcesService;
/*
 * 页面跳转 start
 * =============================================================
 * */	
	@RequestMapping(value="/admin2/permission/toRolesAdd.do")
	public String toRolesAdd(){
		return "redirect:rolesAdd.jsp";
	}
	

	@RequestMapping(value="/admin2/permission/toRolesList.do")
	public String toRolesList(Model model){
		List<Roles> roles=rolesService.getRolesList(0, 1000);
		System.out.println(roles.size()+"=============");
		model.addAttribute("roleList", roles);
		return "/admin2/permission/rolesList";
	}
	
	
	@RequestMapping(value="/admin2/permission/toRolesModify.do")
	public String toRolesModify(Model model ,@RequestParam(value="id")long id){
		Roles roles=rolesService.getRolesById(id);
		model.addAttribute("role", roles);
		return "/admin2/permission/rolesModify";
	}
	
	
	/*
	 *配置资源
	 * /
	 */
	
	@RequestMapping(value="/admin2/permission/toResourceAllocate.do")
	public String toResourceAllocate(Model model,@RequestParam(value="id")long id){
		List<Resources> resourcesList =resourcesService.getResourcesList(0, 1000);
		model.addAttribute("resourceList", resourcesList);
		model.addAttribute("roleId", id);
		return "/admin2/permission/resourcesAllocation";
	}
	/*
	 *查看资源
	 * /
	 */
	
	@RequestMapping(value="/admin2/permission/toRoleResourcesList.do")
	public String toRolesResources(Model model,@RequestParam(value="id")long id){
		List<Resources> resourcesList =resourcesService.getResourcesListByRole(id, 0, 1000);
		model.addAttribute("resourceList", resourcesList);
		model.addAttribute("roleId", id);
		return "/admin2/permission/roleResourceList";
	}
	
	
	/*
	 * 
	 * =============================================================
	 */

	@RequestMapping(value="/admin2/permission/rolesAdd.do")
	public String ResourceAdd(RolesForm rolesForm) {
		
		Roles roles =new Roles();
		
		try {
			BeanUtils.copyProperties(roles, rolesForm);
			roles.setName("ROLE_"+roles.getName());
			rolesService.createRoles(roles);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return "redirect:toRolesAdd.do";
	}

	

	@RequestMapping(value="/admin2/permission/rolesList.do")
	public String ResourceList(Model model) {
		List<Roles> roles=rolesService.getRolesList(0, 1000);
		model.addAttribute("roleList", roles);
		return "/admin2/permission/rolesList";
	}
	
	@RequestMapping(value="/admin2/permission/rolesDelete.do")
	public String ResourceDelete(@RequestParam(value="id")long id,Model model) {
		rolesService.deleteRolesById(id);
		return "redirect:toRolesList.do";
	}
	
	
	@RequestMapping(value="/admin2/permission/rolesModify.do")
	public String ResourceModify(Model model,RolesForm rolesForm) {
		Roles roles =new Roles();
		try {
			BeanUtils.copyProperties(roles, rolesForm);
			rolesService.modifyRoles(roles);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return "redirect:toRolesList.do";
	}
	
	
	/*
	 *  配置资源，关联角色和资源
	 * */
	
	@RequestMapping("/admin2/permission/resourcesAllocateToRole.do")
	public String ResourcesAllocateToRole(HttpServletRequest request){
		String roleid =request.getParameter("roleId");
		String ids[] =request.getParameterValues("ids");
		if(roleid!=null){
			Long roleId=Long.valueOf(roleid);
			rolesService.ResourcesAllocateToRole(ids, roleId);
		}
		
		System.out.println(ids+"==========ids[]");
		//for(String id:ids)
		//	System.out.println("========="+id+"============");
				
		
		return "redirect:toRolesList.do";
	}
	
	
	/*
	 *取消资源与角色的关联
	 * /
	 */
	
	@RequestMapping("/admin2/permission/cancelRoleResourcesList.do")
	public String CancelRolesResources(HttpServletRequest request){
		System.out.println("haaha");
		/*Long roleid =Long.valueOf(request.getParameter("roleId"));
		Long ids =Long.valueOf(request.getParameter("id"));
		rolesService.CancelRolesResources(ids, roleid);*/
		return "redirect:toRoleResourcesList.do";
	}
	
}
